package Windows;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import Threads.JudgeDeamonThread;
import Util.DatabaseConnection;

public class MainWindow {

	private JFrame frmJoj;
	private JLabel state;
	private static JTextArea log;
	public Connection con=null;
	public boolean start_judge=false;
	private JudgeDeamonThread jdt=null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frmJoj.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
	}

	public void addstate(String s){
		state.setText("运行状态:"+s);
	}
	
	public static void println(String s){
		log.setText(log.getText()+s+"\n");;
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmJoj = new JFrame();
		frmJoj.setTitle("JOJ评测核心主控窗体");
		frmJoj.setBounds(100, 100, 650, 520);
		frmJoj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmJoj.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("这是ZJOJ评测系统的评测核心,请在启动本程序时同时启动tomcat以便实现完整的评测功能");
		lblNewLabel.setBounds(66, 0, 528, 26);
		frmJoj.getContentPane().add(lblNewLabel);
		
		JButton btnNewButton = new JButton("全局配置");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String[] s={"true"};
				ConfigWindow.main(s);
			}
		});
		btnNewButton.setBounds(10, 30, 107, 27);
		frmJoj.getContentPane().add(btnNewButton);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 90, 612, 381);
		frmJoj.getContentPane().add(scrollPane);
		
		log = new JTextArea();
		scrollPane.setViewportView(log);
		
		state = new JLabel("运行状态:准备启动");
		state.setBounds(10, 63, 608, 21);
		frmJoj.getContentPane().add(state);
		
		JButton button = new JButton("开始评测");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				println("评测核心启动开始……");
				if(start_judge==true) {println("评测核心已经启动，请不要再次启动！");}
				String DatabaseResult=DatabaseConnection.reSet();
				if(DatabaseResult.equals("")==true){
					con=DatabaseConnection.getConn();
				}else{
					println("数据库连接错误!");
					println("详细信息:");
					println(DatabaseResult);
					println("评测核心启动失败!");
					return ;
				}
				println("数据库信息:");
				println("连接url:"+DatabaseConnection.dburl);
				println("用户名:"+DatabaseConnection.username);
				println("密码:"+DatabaseConnection.password);
				println("数据库连接正常，数据表结构检查完毕！");
				println("评测守护线程正在启动……");
				jdt=new JudgeDeamonThread();
				jdt.start();
				println("评测守护线程已经启动！");
				println("数据库中的评测任务已经可以正常完成！");
				start_judge=true;
			}
		});
		button.setBounds(130, 30, 107, 27);
		frmJoj.getContentPane().add(button);
		
		JButton button_1 = new JButton("停止评测");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				println("评测核心停止开始……");
				if(start_judge==false) {println("评测核心已经停止，请不要再次停止！");}
				println("评测守护线程正在停止……");
				jdt.stop();
				println("评测守护线程已经停止！");
				println("数据库中的评测任务已经停止完成！");
				start_judge=false;
			}
		});
		button_1.setBounds(250, 30, 107, 27);
		frmJoj.getContentPane().add(button_1);
	}
}
